<template>
  <div :class="{'show':show}" class="header-search">
    <svg-icon v-show="showIcon" class-name="search-icon" icon-class="sousuo-solid" @click.stop="click" />
    <el-select
      ref="headerSearchSelect"
      v-model="search"
      :remote-method="querySearch"
      filterable
      default-first-option
      remote
      placeholder="输入菜单名称"
      class="header-search-select"
      popper-class="header-search-dropdown"
      @change="change"
    >
      <el-option v-for="item in options" :key="item.path" :value="item" :label="item.title.join(' > ')" />
    </el-select>
  </div>
</template>

<script>
import Fuse from 'fuse.js'
import path from 'path'

export default {
  name: 'HeaderSearch',
  data() {
    return {
      search: '',
      show: false,
      showIcon: true,
      options: [],
      searchPool: [],
      fuse: undefined
    }
  },
  computed: {
    menuTree() {
      return this.$store.getters.menuTree
    }
  },
  watch: {
    menuTree() {
      this.searchPool = this.generateMenuTree(this.menuTree)
    },
    searchPool(list) {
      this.initFuse(list)
    },
    show(value) {
      if (value) {
        document.body.addEventListener('click', this.close)
      } else {
        document.body.removeEventListener('click', this.close)
      }
    }
  },
  mounted() {
    this.searchPool = this.generateMenuTree(this.menuTree)
  },
  methods: {
    click() {
      this.show = !this.show
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
        this.showIcon = false
      }
    },
    close() {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
      this.options = []
      this.show = false
      this.showIcon = true
    },
    change(val) {
      this.$router.push(`${val.path}?menuCode=${val.code}`)
      this.search = ''
      this.options = []
      this.$nextTick(() => {
        this.show = false
        this.showIcon = true
      })
    },
    initFuse(list) {
      this.fuse = new Fuse(list, {
        shouldSort: true,
        threshold: 0.4,
        location: 0,
        distance: 100,
        maxPatternLength: 32,
        minMatchCharLength: 1,
        keys: [{
          name: 'title',
          weight: 0.7
        }, {
          name: 'path',
          weight: 0.3
        }]
      })
    },
    generateMenuTree(menuList, basePath = '/', prefixTitle = []) {
      let res = []
      for (const menu of menuList) {
        // skip hidden router
        if (menu.hidden) { continue }
        const data = {
          path: menu.path ? path.resolve(basePath, menu.path) : (menu.url || menu.otherPageUrl),
          title: [...prefixTitle],
          code: menu.menuCode || ''
        }
        if (menu.menuName) {
          data.title = [...data.title, menu.menuName]
          if (menu.redirect !== 'noRedirect') {
            // only push the menu with title
            // special case: need to exclude parent router without redirect
            res.push(data)
          }
        }
        // recursive child menu
        if (menu.childrens) {
          const tempMenu = this.generateMenuTree(menu.childrens, data.path, data.title)
          if (tempMenu.length >= 1) {
            res = [...res, ...tempMenu]
          }
        }
      }
      return res
    },
    querySearch(query) {
      if (query !== '') {
        this.options = this.fuse.search(query)
      } else {
        this.options = []
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;
  position: relative;

  .search-icon {
    cursor: pointer;
    font-size: 16px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 12px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;
    position: absolute;
    left: -180px;
    background-color:#00926d;

    /deep/ .el-input__inner {
      font-size: 12px;
      border-radius: 0;
      border: 0;
      height: 26px;
      line-height: 26px;
      border-radius: 3px;
      padding-left: 10px;
      padding-right: 10px;
      box-shadow: none !important;
      vertical-align: middle;
    }

    /deep/ .el-input__suffix{
      .el-icon-search{
        color: #00926D;
      }
    }
  }

  &.show {
    .header-search-select {
      width: 180px;
      margin-left: 10px;
    }
  }
}
</style>
<style lang="less">
  .header-search-dropdown{
    top: 40px !important;
  }
</style>

